<?php

namespace app\controller;
use think\facade\Db;
use think\Request;

class Xhsdevice extends Common
{
    public function read(Request $request)
    {
			if($this->hasPermission($request->sessInfo, 'xhsdevice:read')) {
				return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
			}

			$where = [];
			$limit = $request->param('limit/d', 20);
			$order = $request->param('order', 'desc', 'trim');
			$sort = $request->param('sort', 'status asc, id desc', 'trim');

			$name = $request->param('name', '', 'trim');
			if (empty($name) == false) {
					array_push($where, ['name', 'like', "%{$name}%"]);
			}

			$userStat = $request->param('userStat/d');
			if ($userStat === 0 || $userStat === 1 || $userStat === 2) {
					array_push($where, ['status', '=', $userStat]);
			}

			$toArray = Db::name('cfg_xhs_keywords')->where('datetime', '>', time() - 86400)->field('`deviceId`')->group('deviceId')->select()->toArray();
			foreach ($toArray as $value) {
				if($value['deviceId']) {
					$query = Db::table('cfg_xhs_device')->where('deviceId', $value['deviceId'])->value('id');
					if(empty($query)) {
						Db::name('cfg_xhs_device')->insert(['status' => 2, 'deviceId' => $value['deviceId'], 'create' => time() ]);
					}
				}
			}

			Db::name('cfg_xhs_device')->where('datetime', 'null')->update(['status' => 2]);
			Db::name('cfg_xhs_device')->where('datetime', '<', time() - 3600 * 4)->update(['status' => 2]);

			$paginate = Db::name('cfg_xhs_device')->field('`id`, `name`, `status`, `deviceId`, `remark`, FROM_UNIXTIME(`create`, \'%Y-%m-%d %H:%i:%s\') as `create`, FROM_UNIXTIME(`datetime`, \'%Y-%m-%d %H:%i:%s\') as `datetime`')
			  ->where($where)->order($sort, $order)->paginate($limit);

			$xhsBazaar = Db::table('cfg_xhs_bazaar')->field('id, name')->where('status', 1)->select()->toArray();

			return json(['code' => 0, 'msg' => '', 'count' => $paginate->total(), 'data' => $paginate->items(), 'bazaar' => $xhsBazaar]);
    }

		/**
		 * 创建数据
		 *
		 * @return
		 */
		public function update(Request $request) {
		if($this->hasPermission($request->sessInfo, 'xhsdevice:update')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$id = $request->param('id', '', 'trim');
		$name = $request->param('name', '', 'trim');
		$remark = $request->param('remark', '', 'trim');
		Db::name('cfg_xhs_device')->where('id', $id)->update([ 'name' => $name, 'remark' => $remark]);
		return ['code'=>0, 'msg'=>'操作成功'];;
		}

		/**
		 * 删除数据
		 *
		 * @return
		 */
		public function delete(Request $request) {
			if($this->hasPermission($request->sessInfo, 'xhsdevice:delete')) {
				return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
			}
			$param = $request->param();
			if( !isset($param['id']) ) {
				return ['code'=> 403, 'msg'=> '操作失败!'];
			}
			$index = 0;
			$array = explode(',', $param['id']);
			foreach ($array as $value) {
				$index++;
				Db::table('cfg_xhs_device')->where('id', $value)->delete();
			}
      return json(['code' => 0, 'msg' => '成功删除' . $index . '个设备']);
		}

}
